<template>
  <div id="orgMain">
    <Modal v-model="modal.show"  :loading="modal.loading" :title="modal.title"  :mask-closable="false" :footer-hide=true width="700px" :closable="false">
      <Form ref= "authorizationEdit" :model="rowData" label-position="right" :label-width="80" :rules="rules">
        <FormItem label="菜单编号" prop="atCode"><Input v-model="rowData.atCode" :readonly="modal.readonly"></Input></FormItem>
        <FormItem label="菜单名称" prop="atName"><Input v-model="rowData.atName" :readonly="modal.readonly"></Input></FormItem>
        <FormItem label="上级菜单"><AuthorizationTree v-model="rowData.parentId" width="200px" ref="edtParent"></AuthorizationTree></FormItem>
        <FormItem>
          <div style="float: right;">
            <Button v-show="modal.action!='VIEW'" type="primary"  @click="save" >提交</Button>
            <Button @click="close">取消</Button>
          </div>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import AuthorizationTree from '@/view/Authorization/AuthorizationTree';
import Authorization from '@/api/Authorization';
import {apiResult} from "@/libs/util";
export default {
  name: "AuthorizationEdit",
  components: {AuthorizationTree},
  data(){
    return{
      api: new Authorization(),
      modal:{
        show:false,
        loading:false,
        title:"浏览",
        action:"VIEW",
        callback:null,
        readonly:false
      },
      rowData:{
        id: null,
        atCode: null,
        atName: null,
        parentId: null
      },
      rules:{
        atCode:[{required: true, message: '编号必须输入', trigger: 'blur'}],
        atName:[{required: true, message: '名称必须输入', trigger: 'blur'}]
      }
    }
  },
  methods:{
    openDialog: function (action,row, callback) {
      this.modal.show=true;
      this.modal.readonly=false;
      this.modal.callback = callback;
      this.modal.show = true;
      this.modal.action = action;
      if (action=="ADD"){
        this.rowData.id=null;
        this.rowData.atCode=null;
        this.rowData.atName=null;
        this.rowData.parentId=null;
        this.modal.title="新增";
        this.$refs["authorizationEdit"].resetFields();
        if (!!row){
          this.rowData.parentId = row.id;
        }
      }else if (action=="VIEW"){
        this.modal.title="浏览";
        this.$refs["authorizationEdit"].resetFields();
        this.rowToData(row);
        this.modal.readonly=true;
      }else if (action=="EDIT"){
        this.modal.title="修改";
        this.rowToData(row);
      }

    },
    save(){
      let that = this;
      this.$refs["authorizationEdit"].validate((valid) => {
        if (that.modal.action == "VIEW"){that.close();}
        if (valid) {
          /** 新增 */
          if (that.rowData.id == null){
            this.api.add(JSON.stringify(that.rowData)).then(res => {
              apiResult(res.data,true,result=>{
                if (!!that.modal.callback) that.modal.callback(result.data);
                //that.$Message.info(this.$t('save_new_data_succeed'));
                that.close();
              })
            })
          }else{
            this.api.update(JSON.stringify(that.rowData)).then(res => {
              apiResult(res.data,true,result=>{
                if (!!that.modal.callback) that.modal.callback(result.data);
                that.close();
              })
            })
          }
        } else {
          that.$Message.error(this.$t());
          return;
        }
      })
    },
    close(){
      this.modal.show=false;
      this.$refs["authorizationEdit"].resetFields();
    },
    rowToData(row){
      this.rowData.id=row.id;
      this.rowData.atCode=row.atCode;
      this.rowData.atName=row.atName;
      this.rowData.parentId=row.parentId;
    }
  }
}
</script>
<style lang="less">
@import ".././template";
</style>
